#import "FDHighScoresView.h"

@implementation FDHighScoresView

-(id)initWithFrame:(struct CGRect)frame {
	if ((self == [super initWithFrame: frame]) != nil) 
	{
		NSLog(@"GameMenuView %s", _cmd);
		_contentRect = [UIHardware fullScreenApplicationContentRect];
		_offScreenRect = frame;
		_onScreenRect = frame;
		_onScreenRect.origin.x = 0.0f;
	
		defaults = [[FDDefaultsController alloc] init];

		UIImageView *background = [[UIImageView alloc] initWithFrame:CGRectMake(7,0,309,398)];
		[background setImage:[UIImage applicationImageNamed:@"banner.png"]];	
		[self addSubview:background];
		[background release];
		
		// The title High Scores
		UITextLabel	*cardLabel = [[UITextLabel alloc] initWithFrame:CGRectMake(29, 28, 263, 35)];
		[cardLabel setText:@"High Scores"];
		[cardLabel setCentersHorizontally:YES];
		[cardLabel setShadowOffset:CGSizeMake(0,1)];
		
		struct __GSFont *complFont = [NSClassFromString(@"WebFontCache") createFontWithFamily:@"Georgia" traits:2 size:20];
		[cardLabel setFont:complFont];
		
		float clearColor[4] = {1, 1, 1, 0};
		[cardLabel setBackgroundColor: CGColorCreate(CGColorSpaceCreateDeviceRGB(), clearColor)];
		[self addSubview:cardLabel];
		[cardLabel release];
		
		UIView	*line = [[UIView alloc] initWithFrame:CGRectMake(29,64,263,1)];

		float blackColor[4] = {0, 0, 0, 1};
		[line setBackgroundColor: CGColorCreate(CGColorSpaceCreateDeviceRGB(), blackColor)];
		[self addSubview:line];
		[line release];
				
		[self setEnabled:YES];
		[self setTapDelegate:self];
		
		[self createHighScoresTable];
	}
	return self;
}

#pragma mark Show and Hide Methods
- (void)show
{
	translate = [[UITransformAnimation alloc] initWithTarget:self];
	animator = [[UIAnimator alloc] init];

	struct CGAffineTransform trans = CGAffineTransformMakeTranslation(_contentRect.size.width, 0);
	
	[self setFrame:_offScreenRect];
	[translate setStartTransform:CGAffineTransformMake(1,0,0,1,0,0)];
	[translate setEndTransform:trans];
	[animator addAnimation:translate withDuration:.5 start:YES];

	[highScoresTable scrollRowToVisible:0];
}

- (void)hide
{
	translate = [[UITransformAnimation alloc] initWithTarget:self];
	animator = [[UIAnimator alloc] init];
	
	struct CGAffineTransform trans = CGAffineTransformMakeTranslation(_contentRect.size.width, 0);
	
	[self setFrame:_onScreenRect];
	[translate setStartTransform:trans];
	[translate setEndTransform:CGAffineTransformMake(1,0,0,1,0,0)];
	[animator addAnimation:translate withDuration:.5 start:YES];
	[delegate hidingHighScores];
}

#pragma mark HighScore Table
- (void)createHighScoresTable {
		UITableColumn *position = [[UITableColumn alloc]
			initWithTitle:@"Position"
			identifier:@"position"
			width:40];		
	
		UITableColumn *names = [[UITableColumn alloc]
			initWithTitle:@"Names"
			identifier:@"names"
			width:170];
			
		UITableColumn *scores = [[UITableColumn alloc]
			initWithTitle:@"Scores"
			identifier:@"scores"
			width:50];
				 
		NSLog(@"HighScores >> init table...");
		highScoresTable = [[UITable alloc] initWithFrame:CGRectMake(29, 65, 263, 315)];

		[highScoresTable addTableColumn: position];
		[highScoresTable addTableColumn: names];
		[highScoresTable addTableColumn: scores];
		
		[position release];
		[names release];
		[scores release];
		
		[highScoresTable setSeparatorStyle:1];
		[highScoresTable setDelegate: self];
		[highScoresTable setDataSource: self];
		[highScoresTable setRowHeight:38.0];

		float _clearColor[4] = {1, 1, 1, 0};
		[highScoresTable setBackgroundColor: CGColorCreate(CGColorSpaceCreateDeviceRGB(), _clearColor)];
		
		[self reloadData];				
		[self addSubview: highScoresTable];
		[highScoresTable release];
}

- (UITableCell *)table:(UITable *)table cellForRow:(int)row column:(UITableColumn *)col
{
	NSLog(@"%s", _cmd);

	NSMutableArray		*array = [[NSMutableArray alloc] init];
	[array setArray:[defaults highScoresSorted]];	
	NSDictionary *dict = [NSDictionary dictionaryWithDictionary:[array objectAtIndex:row]];
	
	UIImageAndTextTableCell *cell = [[UIImageAndTextTableCell alloc] init];
	[cell setShowSelection:NO];
	
	if ([[col identifier] isEqualToString:@"names"]) {
		NSLog(@"%s HS Cell Name: %@", _cmd,[dict objectForKey:@"Name"]);
		[cell setTitle:[dict objectForKey:@"Name"]];
	} else if ([[col identifier] isEqualToString:@"scores"]) {
		[cell setTitle:[[dict objectForKey:@"Score"] stringValue]];
	}else if ([[col identifier] isEqualToString:@"position"]) {
		[cell setTitle:[NSString stringWithFormat:@"%i",row+1]];
	}
	
	[array release];
	[cell autorelease]; // We can't keep allocing these things and not releasing them
	return cell;
}

- (void)reloadData
{
	[highScoresTable reloadData];
}

- (void)tableRowSelected:(NSNotification *)notification 
{
	NSLog(@"%s", _cmd);
}

- (int)numberOfRowsInTable: (UITable *)table
{
	return [[NSArray arrayWithArray:[defaults highScoresSorted]] count];
}

#pragma mark Tap Delegate
- (void)mouseUp:(struct __GSEvent *)event {
	[self hide];
}

#pragma mark Delegate
- (id)delegate
{
	return delegate;
}

- (void)setDelegate:(id)newDelegate
{
	delegate = newDelegate;
}

#pragma mark Dealloc
- (void)dealloc
{
	[animator release];
	[translate release];
	[super dealloc];
}
@end